package com.example.heimashop.viewmodel;

import androidx.lifecycle.MutableLiveData;

import com.example.heimashop.api.RetrofitClient;
import com.example.heimashop.base.BaseViewModel;
import com.example.heimashop.model.Product;
import com.example.heimashop.model.ProductDetail;

import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.schedulers.Schedulers;

/**
 * 商品ViewModel
 */
public class ProductViewModel extends BaseViewModel {

    public MutableLiveData<List<Product>> productsLiveData = new MutableLiveData<>();
    public MutableLiveData<ProductDetail> productDetailLiveData = new MutableLiveData<>();
    public MutableLiveData<String> errorLiveData = new MutableLiveData<>();

    /**
     * 搜索商品
     */
    public void searchProducts(String query, int page) {
        addDisposable(RetrofitClient.getInstance()
                .getApiService()
                .searchProducts(query, page, 10)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        response -> {
                            if (response.isSuccess() && response.getMessage() != null) {
                                productsLiveData.setValue(response.getMessage().getGoods());
                            } else {
                                errorLiveData.setValue(response.getMeta() != null ? response.getMeta().getMsg() : "获取失败");
                            }
                        },
                        throwable -> errorLiveData.setValue(throwable.getMessage())
                ));
    }

    /**
     * 获取商品列表
     */
    public void getProductList(int categoryId, int page) {
        addDisposable(RetrofitClient.getInstance()
                .getApiService()
                .getProductList(categoryId, page, 10)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        response -> {
                            if (response.isSuccess() && response.getMessage() != null) {
                                productsLiveData.setValue(response.getMessage().getGoods());
                            } else {
                                errorLiveData.setValue(response.getMeta() != null ? response.getMeta().getMsg() : "获取失败");
                            }
                        },
                        throwable -> errorLiveData.setValue(throwable.getMessage())
                ));
    }

    /**
     * 获取商品详情
     */
    public void getProductDetail(int productId) {
        addDisposable(RetrofitClient.getInstance()
                .getApiService()
                .getProductDetail(productId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        response -> {
                            if (response.isSuccess()) {
                                productDetailLiveData.setValue(response.getMessage());
                            } else {
                                errorLiveData.setValue(response.getMeta() != null ? response.getMeta().getMsg() : "获取失败");
                            }
                        },
                        throwable -> errorLiveData.setValue(throwable.getMessage())
                ));
    }
}
